<template>
  <el-dialog :title="title" :visible.sync="isShow" width="50%" @close="close">
    {{ nodeData.name }}
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeesList">
          <el-option
            v-for="item in emploeesList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <span slot="footer">
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="btnOk">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimpleData } from '@/api/employees.js'
import {
  getDepartmentListData,
  addDepartmentData,
  getDepartmentInfoData,
  editDepartmentData } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    // 点击行的数据
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    // 部门名称查重
    const checkNameRepeat = async(rules, value, cb) => {
      // 1.找到所有部门数据
      const { depts } = await getDepartmentListData()
      let isRepeat = false
      if (this.formData.id) {
        // 2.找打兄弟 pid 相同且不是自己
        const childArr = depts.filter(item => item.pid === this.nodeData.pid && item.id !== this.formData.id)
        // 3.判断是否同名
        isRepeat = childArr.some(item => item.name === value)
      } else {
        // 2.找到当前点击部门的所有子部门数据
        const childArr = depts.filter(item => item.pid === this.nodeData.id)
        // 判断是否重名
        isRepeat = childArr.some(item => item.name === value)
      }

      return isRepeat ? cb(new Error(`同级已经存在${value}这个部门了`)) : cb()
    }
    // 部门编码查重
    const checkCodeRepeat = async(rules, value, cb) => {
      // 1.找到所有部门数据
      const { depts } = await getDepartmentListData()
      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value === value)
      } else {
        // 2.判断编码是否同名
        isRepeat = depts.some(item => item.code === value)
      }

      return isRepeat ? cb(new Error(`已经存在这个部门了`)) : cb()
    }

    return {
      isShow: false,
      // 表单数据对象
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 表单验证规则对象
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      // 员工列表
      emploeesList: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '添加子部门'
    }
  },
  created() {
    // this.getEmployeesList()
  },
  methods: {
    async getEmployeesList() {
      const res = await getEmployeeSimpleData()
      this.emploeesList = res
    },
    // 获取部门详情
    async getDepartmentInfo(id) {
      console.log(1111)
      this.formData = await getDepartmentInfoData(id)
    },
    // 提交事件
    async btnOk() {
    // 1.获取校验结果
      await this.$refs.form.validate()
      // 2.发送请求
      if (this.formData.id) {
        // 修改
        await editDepartmentData(this.formData)
      } else {
        await addDepartmentData({ ...this.formData, pid: this.nodeData.id })
      }

      // 3.提示成功
      this.$message.success('操作成功')
      // 4.列表更新
      this.$emit('update')
      // 5.关闭
      this.isShow = false
    },
    // 关闭弹窗触发
    close() {
      // 重置表单
      this.formData = {}
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }

}
</script>

<style>

</style>
